package com.projectdemo.springbootdemo1.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 *
 * @date 2024/11/8
 * @author zhangzw
 **/
public class LoginHandleInterceptor implements HandlerInterceptor {

    //前置处理
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object username = request.getSession().getAttribute("username");
        if(username == null){
            request.getSession().setAttribute("msg","没有登录，请先登录！");
            response.sendRedirect("/zh-CN/login");
            return false;
        }else{
            return true;
        }
    }
}

